/*
https://leetcode-cn.com/problems/search-in-rotated-sorted-array-ii/solution/sou-suo-xuan-zhuan-pai-xu-shu-zu-ii-by-l-0nmp/
 */
public class Solution81 {
    public boolean search(int[] nums, int target) {
        int n=nums.length;
        if (n==0) {
            return false;
        }
        if (n==1) {
            return nums[0]==target;
        }
        int l=0,r=n-1;
        while (l<=r) {
            int mid=(l+r)/2;
            if (nums[mid]==target){
                return true;
            }
            if (nums[l]==nums[mid] && nums[mid]==nums[r]){
                ++l;
                --r;
            }else if(nums[l]<=nums[mid]){
                if (nums[l]<=target && target<nums[mid]){
                    r=mid-1;
                }else{
                    l=mid+1;
                }
            } else {
                if (nums[mid]<target && target<=nums[r]) {
                    l=mid+1;
                }else{
                    r=mid-1;
                }
            }
        }
        return false;
    }

    public static void main(String[] args) {
        System.out.println(new Solution81().search(new int[]{1,0,1,1,1},0));
    }
}
